package com.czp.infosharingplatformapp.controller;

import com.czp.infosharingplatformapp.dto.PostRequest;
import com.czp.infosharingplatformapp.dto.PostResponse;
import com.czp.infosharingplatformapp.dto.UserResponse;
import com.czp.infosharingplatformapp.model.Post;
import com.czp.infosharingplatformapp.service.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/posts")
public class PostController {

    @Autowired
    private PostService postService;

    @Autowired
    private UserPostRecordService userPostRecordService;

    @Autowired
    private RecommendationService recommendationService;

    @Autowired
    private UserService userService;

    // 创建帖子
    @PostMapping
    public PostResponse createPost(@RequestBody PostRequest postRequest) {
        Post post = postService.createPost(
                postRequest.getTitle(),
                postRequest.getContent(),
                postRequest.getUserId(),
                postRequest.getCategoryId(),
                postRequest.getResourceId(),
                postRequest.getTags());
        return postService.convertToPostResponse(post);
    }

    // 获取所有帖子
    @GetMapping
    public List<PostResponse> getAllPosts() {
        return postService.getAllPosts().stream()
                .map(postService::convertToPostResponse)
                .collect(Collectors.toList());
    }

    @DeleteMapping("/{postId}")
    public UserResponse deletePost(@PathVariable Long postId) {
        postService.deletePost(postId);
        return new UserResponse(200, "帖子已成功删除", null);
    }

    // 搜索帖子
    @GetMapping("/search")
    public List<PostResponse> searchPosts(@RequestParam("keyword") String keyword) {
        return postService.searchPosts(keyword).stream()
                .map(postService::convertToPostResponse)
                .collect(Collectors.toList());
    }

    // 筛选帖子
    @GetMapping("/filter")
    public List<PostResponse> filterPostsByCategory(@RequestParam("categoryId") Long categoryId) {
        return postService.filterPostsByCategory(categoryId).stream()
                .map(postService::convertToPostResponse)
                .collect(Collectors.toList());
    }

    @PostMapping("/record/{postId}")
    public UserResponse recordUserPostView(@PathVariable Long postId, @RequestParam Long userId) {
        userPostRecordService.recordView(userId, postId);
        return new UserResponse(200, "用户浏览已记录", userService.getUserById(userId));
    }

    @GetMapping("/recommend/{userId}")
    public List<PostResponse> recommendPostsForUser(@PathVariable Long userId) {
        return recommendationService.recommendPostsForUser(userId).stream()
                .map(postService::convertToPostResponse)
                .collect(Collectors.toList());
    }

    @GetMapping("/{postId}")
    public PostResponse getPostById(@PathVariable Long postId) {
        return postService.convertToPostResponse(postService.getPostById(postId));
    }
}